package com.hoapital.outpbillwork.billWorkHandler;

import com.hoapital.outpbillwork.module.controller.vo.ApplyInfo;
import com.hoapital.outpbillwork.module.entity.ClinicMaster;
import com.hoapital.outpbillwork.module.entity.OutpMedrec;
import com.hoapital.outpbillwork.module.service.LabTestMasterService;
import com.hospital.api.RemoteBillService;
import com.hospital.common.core.exception.ExceptionHelp;


import com.hoapital.outpbillwork.module.controller.vo.OrderInfo;


import com.hospital.common.core.module.controller.vo.CancelOrder;
import com.hospital.common.core.module.controller.vo.Receive;
import com.hospital.common.core.result.SuccessTip;
import com.hospital.common.core.result.Tip;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author lvyao
 * @description 检验计费接口实现类
 * @date 2021/2/25
 */
@Component
public class LabBillJob implements BillWorkHandler {


    @Resource
    private LabTestMasterService labTestMasterService;


    // 静态初使化当前类
    public static LabBillJob labBillJob;

    //注解@PostConstruct，这样方法就会在Bean初始化之后被Spring容器执行
    @PostConstruct
    public void init() {
        labBillJob = this;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Tip billWork(Receive receive, HttpServletRequest request) {
        Tip retTip = new SuccessTip();
        try {
            boolean saveBillLab = labBillJob.labTestMasterService.saveBillLab(receive);
            if (saveBillLab) {
                retTip.setMessage("检验项目计费成功");
            } else {
                retTip.setCode(501);
                retTip.setMessage("检验项目计费失败");
            }
        } catch (Exception r) {
            retTip.setCode(502);
            retTip.setMessage(r.getMessage());
        }
        return retTip;
    }


    @Override
    public Tip orderWork(OrderInfo orderInfo, HttpServletRequest request, RemoteBillService remoteBillService) {
        Tip retTip = new SuccessTip("");
        try {
            Map<String, String> map = labBillJob.labTestMasterService.saveOrderLab(orderInfo);
            boolean saveOrderLab = Boolean.valueOf(map.get("updateFlag"));
            if (saveOrderLab) {
                retTip.setData(map.get("billNo"));
                retTip.setMessage("检验开单保存成功");
            } else {
                retTip.setCode(501);
                retTip.setMessage("检验开单保存失败");
            }
        } catch (Exception e) {
            retTip.setCode(502);
            retTip.setMessage(e.getMessage());
        }
        return retTip;

    }

    @Override
    public Tip outpRegistWork(OrderInfo receive, HttpServletRequest request) throws ExceptionHelp {
        return null;
    }

    @Override
    public Tip outpVisitWork(OrderInfo receive, HttpServletRequest request) throws ExceptionHelp {
        return null;
    }

    @Override
    public Tip newOrderWork(OrderInfo receive, ClinicMaster clinicMaster, OutpMedrec outpMedrec, ApplyInfo applyInfo, HttpServletRequest request, RemoteBillService remoteBillService) throws ExceptionHelp {
        return null;
    }


    @Override
    public Tip orderCancel(CancelOrder CancelOrder, HttpServletRequest request) throws ExceptionHelp {
        Tip retTip = new SuccessTip();
        try {
            boolean saveBillLab = labBillJob.labTestMasterService.cancelLab(CancelOrder);
            if (saveBillLab) {
                retTip.setMessage("检验撤销成功");
            } else {
                retTip.setCode(501);
                retTip.setMessage("检验撤销失败");
            }
        } catch (Exception r) {
            retTip.setCode(502);
            retTip.setMessage(r.getMessage());
        }
        return retTip;
    }

    @Override
    public Tip sendApplyHl7File(OrderInfo orderInfo, String oneKey, String msgType) throws ExceptionHelp {
        return null;
    }

    @Override
    public Tip sendBillHl7Msg(String hosCode, String hosName, String oneKey, String type, String hl7Status) throws ExceptionHelp {
        return null;
    }

    @Override
    public Tip billInpWork(Receive requestVal, HttpServletRequest request) {
        return null;
    }

}
